//
//  LBAddContactViewController.m
//  LBContactListViewController
//
//  Created by LINAICAI on 2017/12/6.
//

#import "LBAddContactViewController.h"
#import <Masonry.h>
#import <LBCoreKit.h>
#import <LBAlert.h>
#import <LBLoadingView.h>
#import "LBContactModel.h"
#import "LBContactManager.h"

@interface LBAddContactViewController ()
@property(nonatomic , strong)UITextField *userNameTextField;
@property(nonatomic , strong)UITextField *phoneTextField;
@property(nonatomic , strong)UITextField *positionTextField;
@property(nonatomic , strong)UITextField *companyTextField;
@property(nonatomic , strong)UIButton *submitButton;
@end

@implementation LBAddContactViewController
- (UIButton *)submitButton{
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitButton setTitle:@"确定" forState:UIControlStateNormal];
        [_submitButton setGradientBlueBackgroundImage];
        _submitButton.layer.cornerRadius = 4.0;
        _submitButton.layer.masksToBounds = YES;\
        [_submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitButton;
}
- (UILabel *)titleLabelWith:(NSString *)title{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 15)];
    label.text = title;
    label.textColor = [UIColor darkBlack];
    label.font = LBFONT(16);
    return label;
}
- (UITextField *)phoneTextField{
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc]init];
        _phoneTextField.placeholder = @"请输入联系人手机号";
        _phoneTextField.textAlignment = NSTextAlignmentRight;
        _phoneTextField.textColor = [UIColor darkBlack];
        _phoneTextField.font = LBFONT(16);
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
        _phoneTextField.leftView = [self titleLabelWith:@"电话"];
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    return _phoneTextField;
}
- (UITextField *)positionTextField{
    if (!_positionTextField) {
        _positionTextField = [[UITextField alloc]init];
        _positionTextField.placeholder = @"请输入职位(选填)";
        _positionTextField.textAlignment = NSTextAlignmentRight;
        _positionTextField.textColor = [UIColor darkBlack];
        _positionTextField.font = LBFONT(16);
        _positionTextField.leftViewMode = UITextFieldViewModeAlways;
        _positionTextField.leftView = [self titleLabelWith:@"职位"];
        
    }
    return _positionTextField;
}
- (UITextField *)companyTextField{
    if (!_companyTextField) {
        _companyTextField = [[UITextField alloc]init];
        _companyTextField.placeholder = @"请输入所在公司(选填)";
        _companyTextField.textAlignment = NSTextAlignmentRight;
        _companyTextField.textColor = [UIColor darkBlack];
        _companyTextField.font = LBFONT(16);
        _companyTextField.leftViewMode = UITextFieldViewModeAlways;
        _companyTextField.leftView = [self titleLabelWith:@"公司"];
        
    }
    return _companyTextField;
}
- (UITextField *)userNameTextField{
    if (!_userNameTextField) {
        _userNameTextField = [[UITextField alloc]init];
        _userNameTextField.placeholder = @"请输入联系人姓名";
        _userNameTextField.textAlignment = NSTextAlignmentRight;
        _userNameTextField.textColor = [UIColor darkBlack];
        _userNameTextField.font = LBFONT(16);
        _userNameTextField.leftViewMode = UITextFieldViewModeAlways;
        _userNameTextField.leftView = [self titleLabelWith:@"姓名"];
        
    }
    return _userNameTextField;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.userNameTextField];
    [self.view addSubview:self.phoneTextField];
    [self.view addSubview:self.positionTextField];
    [self.view addSubview:self.companyTextField];
    [self.view addSubview:self.submitButton];
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.equalTo(self.userNameTextField.mas_bottom).mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    [self.positionTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.equalTo(self.phoneTextField.mas_bottom).mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    [self.companyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.equalTo(self.positionTextField.mas_bottom).mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.equalTo(self.companyTextField.mas_bottom).mas_offset(20);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.view layoutIfNeeded];
    [self.userNameTextField addLineUp:NO andDown:YES andColor:[UIColor gray]];
    [self.phoneTextField addLineUp:NO andDown:YES andColor:[UIColor gray]];
    [self.positionTextField addLineUp:NO andDown:YES andColor:[UIColor gray]];
    [self.companyTextField addLineUp:NO andDown:YES andColor:[UIColor gray]];
    // Do any additional setup after loading the view.
}
- (void)submit:(id)sender{
    [self.view endEditing:YES];
    if (!self.userNameTextField.text.length) {
        [LBAlert say:@"请填写联系人姓名"];
        return;
    }
    else if (![self.userNameTextField.text validateNick]){
        [LBAlert say:@"请填写正确的联系人姓名"];
        return;
    }
    if (!self.phoneTextField.text.length) {
        [LBAlert say:@"请填写联系人手机号码"];
        return;
    }
    else if (![self.phoneTextField.text validateMobileNumber]){
        [LBAlert say:@"请填写正确的手机号码"];
        return;
    }
    WS(weakSelf);
    [LBLoadingView showInView:self.view style:LBLoadingBackgroundStyleLight];
    [LBContactManager addContactWithCompany:self.companyTextField.text
                                                   lable:self.positionTextField.text
                                             mobilePhone:self.phoneTextField.text
                                                    name:self.userNameTextField.text
                                                 success:^(NSString *message,LBContactModel * model)
     {
         [LBLoadingView dismiss];
         [LBAlert say:^(NSInteger answer){
             [weakSelf.navigationController popViewControllerAnimated:YES];
         } message:@"添加成功"];
     } fail:^(NSString *message) {
         [LBLoadingView dismiss];
         [LBAlert say:message];
     }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
